package com.training.bt.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.training.bt.entity.UserTag;
import com.training.bt.mapper.UserTagMapper;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class UserTagService {
    private final UserTagMapper userTagMapper;

    public void saveUserTag(Long userId, List<Long> tagIds) {
        //删除旧关联
        LambdaQueryWrapper<UserTag> wrapper = new LambdaQueryWrapper<UserTag>()
                .eq(UserTag::getUserId, userId);
        userTagMapper.delete(wrapper);

        List<UserTag> newTags = tagIds.stream()
        .map(tagId -> new UserTag(null, userId, tagId,1))
        .collect(Collectors.toList());

        newTags.forEach(userTagMapper::insert);
    }
    
}
